package app.modules.controller;

import app.modules.Vo.FormRequestVo;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/demoHttp")
public class DemoController {

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/test")
    public String test(){
        String url="01242314431eqrqrereq1324241w23432114321";
        return url;
    }

    @PostMapping("/login")
    public String loginResultType1() {
        // 创建 HttpClient 实例
        String result="0";
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost request = new HttpPost("http://218.2.115.226:8001/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");
            //   stringEntity = new StringEntity("{\"acctID\":\"653c99159ca0a3\", \"username\":xsgs036,\"password\":\"20110612\", \"lcid\":2052}");
            // 设置请求实体 {"acctID":"6609e64881ed0c","username":"001","password":"963.8520","lcid":"2052"}
            String jsonPayload = "{\"acctID\":\"653c99159ca0a3\",\"username\":\"xsgs036\",\"password\":\"20110612\",\"lcid\":\"2052\"}";
            StringEntity entity = new StringEntity(jsonPayload);
            request.setEntity(entity);
            request.setHeader("Content-Type", "application/json");
            System.out.println(entity);


            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Status Code: " + statusCode);

                // 获取响应实体
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    // 将响应实体转换为字符串
                    result = EntityUtils.toString(responseEntity);
                    System.out.println("Response Body: " + result);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("返回出来的登录结果"+result);
        return result;
    }
    //查询表单数据接口
    @PostMapping("/getJDYXKView")
    public String getJDYXKView() {
        String LoginResult = loginResultType1();
        System.out.println("调用登录返回结果"+LoginResult);

//        System.out.println("查看表单参数+++++++++++"+formRequestVo);
        String url = "http://218.2.115.226:8001/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc";

        String result = new String();
        // 创建 HttpClient 实例

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost request = new HttpPost(url);


            // 设置请求实体
            String formid = "BD_MATERIAL";
//            String data  = "{\"CreateOrgId\":0,\"Id\":0,\"Number\":\"SVINV00000003\"}";
            String data  = "{\"Number\":\"200-01010101-000008\"}";

            List<Object> objects = new ArrayList<>();
            objects.add(formid);
            objects.add(data);
            String s = objects.toString();
            System.out.println(s);


//            HashMap<Object, Object> map = new HashMap<>();
//            map.put("formid",formid);
//            map.put("data",data);
//            String s = map.toString();
//            System.out.println(s);

            StringEntity entity = new StringEntity(s);
            System.out.println("查看表单参数"+entity);
            request.setEntity(entity);
            request.setHeader("Content-Type", "application/json");

            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Status Code: " + statusCode);

                // 获取响应实体
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    // 将响应实体转换为字符串
                    result = EntityUtils.toString(responseEntity);
                    System.out.println("Response Body: " + result);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
//        return null;
    }


    //查询表单数据接口
    @PostMapping("/getJDYXKView2")
    public String getJDYXKView2(@RequestBody FormRequestVo formRequestVo) {
        String LoginResult = loginResultType1();
        System.out.println("调用登录返回结果"+LoginResult);

        System.out.println("查看表单参数+++++++++++"+formRequestVo);
        String url = "http://218.2.115.226:8001/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc";

        String result = new String();
        // 创建 HttpClient 实例

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost request = new HttpPost(url);


            // 设置请求实体

//            String data  = "{\"CreateOrgId\":0,\"Id\":0,\"Number\":\"SVINV00000003\"}";
            String data  = "{\"Number\":\"200-01010101-000008\"}";
            FormRequestVo requestVo = new FormRequestVo();
            requestVo.setFormid(formRequestVo.getFormid());
            requestVo.setData(data);


            StringEntity entity = new StringEntity(requestVo.toString());
            System.out.println("查看表单参数"+entity);
            request.setEntity(entity);
            request.setHeader("Content-Type", "application/json");
//            request.setHeader("cookies",);

            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Status Code: " + statusCode);

                // 获取响应实体
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    // 将响应实体转换为字符串
                    result = EntityUtils.toString(responseEntity);
                    System.out.println("Response Body: " + result);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }




    //查询表单数据接口
    @PostMapping("/saveOrder")
    public String saveOrder() {
        String LoginResult = loginResultType1();
        System.out.println("调用登录返回结果"+LoginResult);

        String url = "http://218.2.115.226:8001/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc";

        String result = new String();
        // 创建 HttpClient 实例

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost request = new HttpPost(url);


            // 设置请求实体
            String formid = "SAL_OUTSTOCK";

            String data = "{\"Creator\":\"\",\"NeedUpDateFields\":[],\"Model\":" + "{\"FID\":\"0\",\"FStockOrgId\":{\"FNumber\":\"210\"},\"FBillTypeID\":{\"FNumber\":\"XSCKD01_SYS\"},\"FBillNo\":\"CSDGBC21002\",\"FCustomerID\":{\"FNumber\":\"CUST0073\"},\"SubHeadEntity\":{\"FExchangeRate\":6.51},\"FEntity\":[{\"FEntryID\":\"0\",\"FMATERIALID\":{\"FNumber\":\"03.001\"},\"FStockID\":{\"FNumber\":\"CK002\"},\"FRealQty\":324,\"FBaseUnitQty\":324},{\"FEntryID\":\"0\",\"FMATERIALID\":{\"FNumber\":\"03.001\"},\"FStockID\":{\"FNumber\":\"CK004\"},\"FRealQty\":220,\"FBaseUnitQty\":220}]]}}";

            List<Object> objects = new ArrayList<>();
            objects.add(formid);
            objects.add(data);
            String s = objects.toString();
            System.out.println(s);

            StringEntity entity = new StringEntity(s);
            System.out.println("查看表单参数"+entity);
            request.setEntity(entity);
            request.setHeader("Content-Type", "application/json");

            // 发送请求并获取响应
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Status Code: " + statusCode);

                // 获取响应实体
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    // 将响应实体转换为字符串
                    result = EntityUtils.toString(responseEntity);
                    System.out.println("Response Body: " + result);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }
}


